package cst.start.widgettest111hl;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText inputtext;
    private Button textbutton,changebutton,sexbutton;
    private RadioButton radiobutton,radiobutton2;
    private ImageView imageView;
    private ImageButton imagebutton;
    private RadioGroup rg;
    private CheckBox checkBox,checkBox2,checkBox3;
    String msg="";

    
    private void init(){
        inputtext=(EditText)findViewById(R.id.inputtext);
        textbutton=(Button) findViewById(R.id.textbutton);
        changebutton=(Button)findViewById(R.id.changebutton);
        imageView=(ImageView)findViewById(R.id.imageView);
        imagebutton=(ImageButton)findViewById(R.id.imageButton);
        sexbutton=(Button)findViewById(R.id.sexbutton);
        radiobutton=(RadioButton)findViewById(R.id.radioButton);
        radiobutton2=(RadioButton)findViewById(R.id.radioButton2);
        rg=(RadioGroup)findViewById(R.id.radiogroup);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        checkBox2=(CheckBox)findViewById(R.id.checkBox2);
        checkBox3=(CheckBox)findViewById(R.id.checkBox3);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        textbutton.setOnClickListener(this);
        changebutton.setOnClickListener(this);
        imagebutton.setOnClickListener(this);
        sexbutton.setOnClickListener(this);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int i) {
                switch (i){
                    case R.id.radioButton:inputtext.setText("男");break;
                    case R.id.radioButton2:inputtext.setText("女");break;
                }
            }
        });
        checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean i) {
                if (i)  msg += "篮球 ";
                else    msg = msg.replace("篮球 ", "");
                inputtext.setText(msg);
            }
        });
        checkBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean i) {
                if (i)  msg += "足球 ";
                else    msg = msg.replace("足球 ", "");
                inputtext.setText(msg);
            }
        });
        checkBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean i) {
                if (i)  msg += "羽毛球 ";
                else    msg = msg.replace("羽毛球 ", "");
                inputtext.setText(msg);
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.textbutton:
                Toast.makeText(this, "内容是"+inputtext.getText().toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.changebutton:
                imageView.setImageResource(R.drawable.eclipse);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                break;
            case R.id.imageButton:
                Intent intent = new Intent(MainActivity.this,ButtonActivity.class);
                intent.putExtra("para1",inputtext.getText().toString());
                startActivity(intent);
                break;
            case R.id.sexbutton:
                if(radiobutton.isChecked()) inputtext.setText("男");
                if(radiobutton2.isChecked()) inputtext.setText("女");
                break;
        }
    }
}
